package com.fate.aspect;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.fate.anno.Log;
import com.fate.domain.SysLog;
import com.fate.domain.SysUser;
import com.fate.service.SysLogService;
import com.fate.service.SysUserService;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * 切面记录日志
 * 用切面做缓存
 */
@Component
@Aspect
public class LogAspect {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysLogService sysLogService;
    /**
     * 切点
     */
    @Around("@annotation(com.fate.anno.Log)")
    public Object LogAround(ProceedingJoinPoint joinPoint){
     Object result = null;
     //1.拿到方法签名
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        //方法名称
        String methodName = method.getName();
        Log log = method.getAnnotation(Log.class);
        //注解的属性值
        String operation = log.operation();
        //ip
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String ip = request.getRemoteHost();
        System.out.println(ip);
        //目标方法的执行时间
        long start = System.currentTimeMillis();
        try{
            result = joinPoint.proceed(joinPoint.getArgs());
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }
       Long end =  System.currentTimeMillis();
        //记录时间
        long time = end - start;
        //拿到当前用户
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        //需要根据用户的id 查询用户名
        SysUser sysUser = sysUserService.getById(Long.valueOf(userId));
        //组装一个对象SysLog对象插入数据库
        SysLog sysLog = SysLog.builder()
                .username(sysUser.getUsername())
                .createDate(new Date())
                .time(time)
                .ip(ip)
                .operation(operation)
                .method(methodName)
                .params(joinPoint.getArgs() == null ? "" : JSON.toJSONString(joinPoint.getArgs()))
                .build();
        //插入
        sysLogService.save(sysLog);
        return result;




    }


















}
